package day0411;

import java.util.Scanner;

public class Test02 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7};
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入想要查找的数字: ");
        int target = sc.nextInt();
        int left = 0;
        int right = arr.length - 1;
        int result = -1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (arr[mid] == target) {
                result = mid;
                break;
            } else if (arr[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        if (result != -1) {
            System.out.println("目标元素 " + target + " 在数组中的索引是: " + result);
        } else {
            System.out.println("目标元素 " + target + " 不在数组中。");
        }
    }
}
    